package com.zc.angelica.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.angelica.entity.ResponseData;
import com.zc.angelica.mapper.RoleInfoMapper;
import com.zc.angelica.entity.RoleInfo;
import com.zc.angelica.service.RoleInfoService;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * @author makejava
 * @since 2022-04-05 20:17:17
 */
@Service
public class RoleInfoServiceImpl extends ServiceImpl<RoleInfoMapper, RoleInfo> implements RoleInfoService {

    @Override
    public ResponseData select(Integer pageNo, Integer pageSize) {
        if (pageNo != null && pageSize != null) {
            IPage<RoleInfo>  rolePage = new Page<>(pageNo, pageSize);
            return ResponseData.success(this.baseMapper.selectPage(rolePage, null));
        }
        return ResponseData.success(this.baseMapper.selectList( null));

    }

    @Override
    public ResponseData addRole(RoleInfo role) {
        int insert = this.baseMapper.insert(role);
        if (insert == 0) {
            return ResponseData.error("添加角色失败！");
        }
        return ResponseData.success("添加角色成功！");
    }

    @Override
    public ResponseData deleteRole(Serializable id) {
        int delete = this.baseMapper.deleteById(id);
        if (delete == 0) {
            return ResponseData.error("删除角色失败！");
        }
        return ResponseData.success("删除角色成功！");
    }

    @Override
    public ResponseData modifyState(Serializable id, Integer type) {
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.setId((Long) id);
        roleInfo.setRoleStatus(type);
        int modify = this.baseMapper.updateById(roleInfo);
        String state = "启用";
        if (type == 2) {
            state = "禁用";
        }
        if (modify == 0) {
            return ResponseData.error(state + "角色失败！");
        }
        return ResponseData.success(state + "角色成功！");
    }
}

